package org.yjw.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yjw.demo.bean.dto.LoginDto;
import org.yjw.demo.config.Student;
import org.yjw.demo.bean.entity.user.User;
import org.yjw.demo.service.user.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * @author 31898
 */
@Controller
@Slf4j
public class LoginController {

    @Resource
    private Student student;
    @Resource
    private UserService userService;

    /**
     * 登录
     * @return
     */
    @RequestMapping("/login")
    public String login(LoginDto loginDto, Model model) {
        User user = userService.findByName(loginDto.getUsername());
        model.addAttribute("name",user.getUsername());
        model.addAttribute("address",user.getAddress());
        // 重定向到商品列表页面
        return "jsp/welcome";
    }
    /**
     * 注册
     * @return
     */
    @Transactional
    @RequestMapping("/insertZc")
    public String insertZc(LoginDto loginDto, Model model) {
        Long aLong = userService.insert(User.builder().username(loginDto.getUsername()).password("110").build());
        model.addAttribute("name",aLong);
        // 重定向到商品列表页面
        System.out.println(1 / 0);
        return "jsp/welcome";
    }

    /**
     * 退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //清空session
        session.invalidate();
        // 重定向到登录页面
        return "redirect:/login.jsp";
    }
}